#include <iostream>
#include "../comm/httplib.h"
#include "oj_control.hpp"

using namespace httplib;

int main()
{

    Server svr;
    Control ctr;

    //展示题目列表页面
    svr.Get("/all_questions",[&](const Request& req,Response& resp)
    {
        std::string html;
        ctr.AllQuestions(&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //展示题目页面(获取题目的内容)
    //           正则表达式
    svr.Get(R"(/question/(\d+))",[&](const Request& req,Response& resp)
    {
        std::string question = req.matches[1];
        std::string html;
        ctr.OneQuestion(question,&html);
        resp.set_content(html,"text/html;charset=utf-8");
    });

    //展示编译结果页面(用户提交代码，使用我们的判题功能)
    svr.Post(R"(/judge/(\d+))",[&](const Request& req,Response& resp)
    {
        std::string judge_num = req.matches[1];
        std::string result;
        ctr.Judge(judge_num,req.body,&result);
        resp.set_content(result,"application/json;charset=utf-8");
        //resp.set_content("这是一个判题结果: "+judge,"text/plain;charset=utf-8");
    });
    svr.set_base_dir("./wwwroot");

    svr.listen("0.0.0.0",8080);

    return 0;
}